<%@ page import="com.wl.shixun1.entity.Fraction" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>2350300097 万朗</title>
</head>
<body>
<%
    String x = request.getParameter("x");
    String y = request.getParameter("y");
    String z = "";
    String error = ""; // 用于存储错误信息

    if (x != null && y != null && !x.isEmpty() && !y.isEmpty()) {
        try {
            // 处理第一个分数x
            int i = x.indexOf("/");
            if (i == -1 || i == 0 || i == x.length() - 1) {
                throw new RuntimeException("x格式错误，应为“分子/分母”（如3/4）");
            }
            String xNumStr = x.substring(0, i);
            String xDenStr = x.substring(i + 1);
            int xNum = Integer.parseInt(xNumStr);
            int xDen = Integer.parseInt(xDenStr);
            if (xDen == 0) {
                throw new RuntimeException("x的分母不能为0");
            }

            // 处理第二个分数y
            i = y.indexOf("/");
            if (i == -1 || i == 0 || i == y.length() - 1) {
                throw new RuntimeException("y格式错误，应为“分子/分母”（如3/4）");
            }
            String yNumStr = y.substring(0, i);
            String yDenStr = y.substring(i + 1);
            int yNum = Integer.parseInt(yNumStr);
            int yDen = Integer.parseInt(yDenStr);
            if (yDen == 0) {
                throw new RuntimeException("y的分母不能为0");
            }

            // 计算结果
            Fraction xx = new Fraction(xNum, xDen);
            Fraction yy = new Fraction(yNum, yDen);
            z = xx.add(yy).drawl();
        } catch (Exception e) {
            error = "错误：" + e.getMessage();
            z = ""; // 出错时清空结果
        }
    }
%>

<form action="" method="post">
    <input type="text" name="x" id="x" value="<%=x == null ? "" : x%>">
    +
    <input type="text" name="y" id="y" value="<%=y == null ? "" : y%>">
    <input type="submit" name="button" id="button" value="=">
    <input type="text" name="z" id="z" value="<%=z%>">
    <br>
    <span style="color: red;"><%=error%></span> <%-- 显示错误信息 --%>
</form>
</body>
</html>